package com.yeti.controller;

import java.security.Principal;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yeti.domain.Cliente;
import com.yeti.service.AdministradorService;

@Controller @RequestMapping(value="/administrador")
public class AdministradorController {
	
	@Autowired
	AdministradorService administradorService;
	
	@Autowired
	private MessageSource messageSource;

	@RequestMapping(value="/home", method = RequestMethod.GET)
	public String Home(ModelMap model, Principal principal,HttpServletRequest request ) {
		String name = principal.getName();
//		ArrayList<Cliente> clientes = administradorService.getClientes();
//		model.addAttribute("username", name);	
//		model.addAttribute("usuarios", clientes);
		
		
		String pageString = request.getParameter("page");
		int page = 1;
		if(pageString!=null){
			page = Integer.parseInt(pageString);
		}
		ArrayList<Object> clientes = administradorService.getClientes(page);
		model.addAttribute("username", name);
		model.addAttribute("usuarios", clientes.get(0));
		model.addAttribute("page",page);
		model.addAttribute("totalPages", (Integer)clientes.get(1));
		
		
		return "/administrador/show";
	}
	
	@RequestMapping(value="/cambiarEstado", method = RequestMethod.POST)
	public @ResponseBody String cambiarEstado(HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("id"));
		boolean enabled = Boolean.parseBoolean(request.getParameter("enabled"));
		administradorService.setClienteEnabled(id, enabled);
		return "";
	}
	
}
